import { cloneDeep } from 'lodash-es'
const predefineRouteImports = import.meta.glob('/src/views/**/routeMap.ts')

let predefineRouteCache = []
/**
 * 生成预定于的路由
 * 即每个模块下定义的路由设置
 * 
 * 后端只需要和 path 属性对上即可
 */
export const generatePredefineRouteMaps = async (): Promise<RouteConfigsTable[]> => {

  if (predefineRouteCache.length)
    return predefineRouteCache;

  const promises = Object.keys(predefineRouteImports).map((key: string) => new Promise(reslove => {
    predefineRouteImports[key]().then(res => reslove(res.default))
  }));

  const routes = await Promise.all(promises)


  predefineRouteCache = routes.reduce((p, c) => {
    if (Array.isArray(c)) {
      return p.concat(c)
    } else {
      return p.concat([c])
    }
  }, [])

  return cloneDeep(predefineRouteCache);
}